package leetcode.L19;
public class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      public ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        //使用双指针
        if (head==null) {
            return null;
        }
        //考虑移除head的问题
        ListNode dummy = new ListNode(0,head);
        ListNode first = dummy;
        ListNode second = dummy;
        int k = 0;
        boolean flag = false;
        while (first != null) {
            first = first.next;
            if (flag) {
                second = second.next;
            }
            if (k==n) {//这里多走了一步
                flag = true;
            }
            k++;
        }
        //移除
        second.next=second.next.next;
        return dummy.next;
    }

    public static void main(String[] args) {
        ListNode s = new ListNode(1, null);
        Solution solution = new Solution();
        solution.removeNthFromEnd(s, 1);
    }
}